/**
 * Created by Liutong on 12/28/15.
 */
(function(window,document){
    $(document).ready(function(){
        $('form').bootstrapValidator();
        $(".btn-upload").click(function(){
            var id = $(this).data("upload");
            $("#"+id).click();
        });

        //上传图片
        $(".input-file").change(function (evt) {
            var files = evt.target.files;
            var $uploadTarget = $("#img_"+$(this).attr("name"));
            for(var i = 0;i<files.length;i++){
                var f = files[i];
                if (!(f.type.indexOf("image") == 0)){
                    alert(f.name + "不是图片类型!");
                    continue;
                }
                var reader = new FileReader();
                reader.onload = (function(filetemp){
                    return function(e){
                        $uploadTarget.attr("src", e.target.result);
                    }
                })(f);
                reader.readAsDataURL(f);
            }
        });

        $('#order').numeric();
        $('#myForm').bootstrapValidator({
    //        live: 'disabled',
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                "manufacturer.code": {
                    group: '.col-sm-6',
                    validators: {
                        notEmpty: {
                            message: '品牌编码不能为空'
                        },
                        regexp: {
                            regexp: /^[a-zA-Z0-9_\.]+$/,
                            message: '品牌编码错误，只允许字母,数字,_'
                        }
                    }
                },
                "manufacturer.tbSellerId":{
                    group: '.col-sm-6',
                    validators: {
                        regexp: {
                            regexp: /^[0-9\.]+$/,
                            message: '淘宝卖家ID必须为数字'
                        }
                    }
                },
                "description.name": {
                    group: '.col-sm-6',
                    validators: {
                        notEmpty: {
                            message: '品牌名称不能为空'
                        }
                    }
                }
            }
        });

        $("#description").bind("input propertychange",function(){
            var restCount = 350 - getByteLen($("#description").val());
            if(restCount <= 0){
                $("#descChartCount").text("0");
                $(this).val(cutStrForNum($(this).val(),350));
            } else {
                $("#descChartCount").text(restCount);

            }
        });

        //描述剩余字符数
        $("#descChartCount").text(350 - getByteLen($("#description").val()));
    });

    var getByteLen = function (val) {
        var len = 0;
        for (var i = 0; i < val.length; i++) {
            if (val[i].match(/[^x00-xff]/ig) != null) //全角
                len += 2;
            else
                len += 1;
        };
        return len;
    };

    var cutStrForNum = function (str, num) {
        var len = 0;
        for (var i = 0; i < str.length; i++) {
            if (str[i].match(/[^x00-xff]/ig) != null) //全角
                len += 2;
            else
                len += 1;
            if (len >= num) {
                return str.substring(0, i+1);
            }
        }
        return str;
    };
})(window,document);